//
//  SquarePostsDetailsReplylTableView.m
//  安好
//
//  Created by 芮连朋 on 14/11/23.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SquarePostsDetailsReplylTableView.h"
#import "SpuareDataModels.h"
@implementation SquarePostsDetailsReplylTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self.rect = frame;
    self = [super initWithFrame:frame style:style];
    if (self) {
        


    }
    return self;


}
-(void)setArr:(NSArray *)arr{
    if (!arr||arr.count==0) {
        return;
    }
    _arr =arr;
[self loadTableView];
}
-(void)loadTableView{


    
    NSMutableArray *arrMassage = [[NSMutableArray alloc]initWithCapacity:10];
    NSMutableArray *arrnameA = [[NSMutableArray alloc]initWithCapacity:10];
    NSMutableArray *arrnameB = [[NSMutableArray alloc]initWithCapacity:10];

    
    
    
    for (int i=0; i<self.arr.count;i++) {
        SquareDetailsQuote*sd =[self.arr objectAtIndex:i];
        [arrMassage addObject:[NSString stringWithFormat:@"%@回复%@:%@",sd.sendusername,sd.recvusername,sd.message,nil]];
        
//        [arrMassage addObject:[self.arr objectAtIndex:i]];
        
        
//            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, [self hightWithData:sd.message nameA:sd.sendusername nameB:sd.recvusername]);
        [self hightWithData:sd.message nameA:sd.sendusername nameB:sd.recvusername];
        
        
    }
    
    
//    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, [self hightWithData:arrMassage widht:self.rect.size.width]+15);
    


    
    
    self.scrollEnabled = NO;
    [self setSeparatorStyle:UITableViewCellSeparatorStyleNone];//去掉系统的分割线

    self.delegate = self;
    self.dataSource = self;


}
//-(CGFloat)hightWithData:(NSArray *)dataArr widht:(CGFloat)width{
//    
//    CGFloat hight = 0;
//    for (int i=0; i<dataArr.count; i++) {
//        NSString *dataStr = [[NSString alloc]initWithFormat:@"%@",[dataArr objectAtIndex:i]];
//        
//        
//        NSString *contentStr =[NSString stringWithFormat:@"%@",dataStr];
//        // contentStr为你要显示的图文字符串
//        NSString *str = [HtmlString transformString:contentStr];
//        
//        RCLabel*textLabel = [[RCLabel alloc]initWithFrame:CGRectMake(0,0,kScreenWidth-(15+2)*2,0)];
////        NSLog(@"%f",self.frame.size.width-4*2);
//        //    textLabel.delegate =self;
//        //    textLabel.sizeDelegate = self;
//        [textLabel setFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13]];
//        RCLabelComponentsStructure *componentsDS = [RCLabel extractTextStyle:str];
//        textLabel.componentsAndPlainText = componentsDS;
//        CGSize optimalSize = [textLabel optimumSize:YES];   //计算图文混排后的高度
//        textLabel.frame=CGRectMake(0,textLabel.frame.origin.y,optimalSize.width, optimalSize.height);
//        textLabel.lineBreakMode =NSLineBreakByCharWrapping;
//        textLabel.backgroundColor = [UIColor clearColor];
//        textLabel.textColor =[UIColor orangeColor];
//        
//        textLabel.textColor = [UIColor colorWithRed:33.0/255 green:33.0/255 blue:33.0/255 alpha:1];
//        
//        hight+=textLabel.frame.size.height;
//    }
//    return hight+2+2;
//    
//    
//    
//}



-(void)hightWithData:(NSString *)dataStr nameA:(NSString *)nameA nameB:(NSString *)nameB{
    
    
    
    
    NSString *contentStr =[NSString stringWithFormat:@"%@",dataStr];
    // contentStr为你要显示的图文字符串
    NSString *str = [HtmlString transformString:contentStr peopleA:nameA peopleB:nameB];
    
    RCLabel*textLabel = [[RCLabel alloc]initWithFrame:CGRectMake(0,4,kScreenWidth-(15+2)*2,0)];
    //    textLabel.delegate =self;
    //    textLabel.sizeDelegate = self;
    [textLabel setFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13]];
    RCLabelComponentsStructure *componentsDS = [RCLabel extractTextStyle:str];
    textLabel.componentsAndPlainText = componentsDS;
    CGSize optimalSize = [textLabel optimumSize:YES];   //计算图文混排后的高度
    textLabel.frame=CGRectMake(0,textLabel.frame.origin.y,kScreenWidth-(15+2)*2, optimalSize.height);
    textLabel.lineBreakMode =NSLineBreakByCharWrapping;
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textColor =[UIColor orangeColor];
    
    textLabel.textColor = TITLE_ARTICLE_CONTENTFONT_COLOR;
    
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height+textLabel.frame.size.height+1+1);
    
    
    
}


#pragma mark: UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger  numbers=self.arr.count;
    if (numbers>0) {
//        [self.delegate1 squarePostsDetailsReplylTableViewWithHight:tableView.contentSize.height];
    }

    return numbers;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat cellHight = 2+[self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height+2;
    
    return cellHight;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellWithIdentifier = @"Cell";
    SquarePostsDetailsReplylTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[SquarePostsDetailsReplylTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
    }
//    SquareDetailsQuote*sd =[self.arr objectAtIndex:indexPath.row];
//    [cell loadheaderData:[NSString stringWithFormat:@"%@:%@",[sd sendusername],[sd message],nil]];
    
    
    
    
    
//    [cell loadheaderData:[NSString stringWithFormat:@"%@:%@",[[self.arr objectAtIndex:indexPath.row] objectForKey:@"sendusername"],[[self.arr objectAtIndex:indexPath.row] objectForKey:@"message"],nil]];
//    [cell loadheaderData:[NSString stringWithFormat:@"%@",[[self.arr objectAtIndex:indexPath.row] objectForKey:@"message"],nil] nameA:[NSString stringWithFormat:@"%@",[[self.arr objectAtIndex:indexPath.row] objectForKey:@"recvusername"],nil] nameB:@""];
    
    SquareDetailsQuote  *squareDetailsQuote =[self.arr objectAtIndex:indexPath.row];
    [cell loadheaderData:[NSString stringWithFormat:@"%@",squareDetailsQuote.message] nameA:[NSString stringWithFormat:@"%@",squareDetailsQuote.sendusername] nameB:[NSString stringWithFormat:@"%@",squareDetailsQuote.recvusername]];

    
    
    
    

    //计算总高度
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, tableView.contentSize.height);
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *ta = [tableView cellForRowAtIndexPath:indexPath];
    CGPoint point = CGPointMake(ta.frame.origin.x, ta.frame.origin.y+ta.frame.size.height);
    MyNSLog("point = %lf,%lf\n", point.x, point.y);

    
    CGPoint tabelPoint = [self convertPoint:point fromView:self];
    MyNSLog("tabelPoint = %lf,%lf\n", tabelPoint.x, tabelPoint.y);
    
    
    
    //获取到点击cell的父视图的tableview 上的坐标点
    CGPoint cellPoint = [self convertPoint:point fromView:self];
//    MyNSLog("tabelPoint = %lf,%lf\n", tabelPoint.x, tabelPoint.y);
    
    
    tableView.tag=self.index;
    MyNSLog("%d,%d", self.index,indexPath.row);
    
    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInteger:indexPath.row],@"index",tableView,@"tableView",[NSValue valueWithCGPoint:cellPoint],@"cellPoint", nil];
    
    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"SELECTCELL" object:self userInfo:dict];
    
    
//    alertContent(@"正在自定义键盘 目前还没评论功能");
}

//#pragma mark: RCLabelDelegate
//-(void)RCLabel:(id)RCLabel didSelectLinkWithURL:(NSString *)url
//{
//    
////    NSLog(@"url======%@",url);
//}
//#pragma mark: RCLabelSizeDelegate
//
//- (void)RCLabel:(id)RCLabel didChangedSize:(CGSize)size{
////    NSLog(@"%f,%f",size.width,size.height);
//}



@end
